<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="6" :xs="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <!-- 左侧信息展示 -->
            <span>个人信息</span>
          </div>
          <div>
            <div class="text-center">
              <userAvatar />   <!-- 嵌套头像组件 -->
            </div>
            <ul class="list-group list-group-striped">
              <li class="list-group-item">
                <svg-icon icon-class="user" />用户名称
                <div class="pull-right">{{ user.userName }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="phone" />手机号码
                <div class="pull-right">{{ user.phonenumber || (employee && employee.mobile) || '未设置' }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="email" />用户邮箱
                <div class="pull-right">{{ user.email || (employee && employee.email) || '未设置' }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="tree" />所属部门
                <div class="pull-right" v-if="user.dept">{{ user.dept.deptName }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="peoples" />所属角色
                <div class="pull-right">{{ roleGroup }}</div>
              </li>
              <li class="list-group-item">
                <svg-icon icon-class="date" />创建日期
                <div class="pull-right">{{ user.createTime }}</div>
              </li>
            </ul>
          </div>
        </el-card>
      </el-col>
      <el-col :span="18" :xs="24">
        <el-card>
          <div slot="header" class="clearfix">
            <!-- 右侧标签页 -->
            <span>基本资料</span>
          </div>
          <el-tabs v-model="selectedTab">
            <el-tab-pane label="基本资料" name="userinfo">
              <userInfo :user="user" />   <!-- 嵌套用户信息组件 -->
            </el-tab-pane>
            <el-tab-pane label="修改密码" name="resetPwd">
              <resetPwd />    <!-- 嵌套密码组件 -->
            </el-tab-pane>
            <el-tab-pane label="证书管理" name="certificates">
              <certificates :emp-id="employee.empId" @close="handleClose" />
            </el-tab-pane>
            <el-tab-pane label="教育背景" name="education">
              <education :emp-id="employee.empId" @close="handleClose" />
            </el-tab-pane>
            <el-tab-pane label="工作经历" name="workExperience">
              <work-experience :emp-id="employee.empId" @close="handleClose" />
            </el-tab-pane>
            <el-tab-pane label="我的假期" name="leaveBalance">
              <leave-balance :emp-id="employee.empId" @close="handleClose" />
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// 导入子组件
import userAvatar from "./userAvatar"
import userInfo from "./userInfo"
import resetPwd from "./resetPwd"
import Certificates from "./Certificates"
import Education from "./Education"
import WorkExperience from "./WorkExperience"
import LeaveBalance from "./LeaveBalance";
import {getUserProfile, listUser} from "@/api/system/user"
import { getEmployeeByUserId } from "@/api/hrm/employee"

export default {
  name: "Profile",
  components: {
    userAvatar,
    userInfo,
    resetPwd,
    Certificates,
    Education,
    WorkExperience,
    LeaveBalance
  },   // 注册子组件
  data() {
    return {
      selectedTab: 'userinfo',
      employee: {},
      user: {},
      roleGroup: {},
      postGroup: {},
    }
  },
  created() {
    this.getUser()
  },
  computed: {
    validEmployeeId() {
      // 尝试从多个可能的位置获取employeeId
      return Number(this.employee?.employeeId) ||
        Number(this.user?.employeeId) ||
        null
    }
  },
  methods: {
    async getUser() {
      try {
        // 1. 获取用户基本信息
        const userRes = await getUserProfile()
        this.user = userRes.data || {}

        // 2. 如果有 userId，查询员工信息
        if (this.user.userId) {

          getEmployeeByUserId(this.user.userId).then(response => {
            this.employee = response.data;
          })
        }

        // 3. 处理角色和岗位信息（示例）
        this.roleGroup = (this.user.roles || []).map(r => r.roleName).join(', ')
        this.postGroup = (this.user.posts || []).map(p => p.postName).join(', ')

      } catch (error) {
        console.error("加载用户或员工信息失败:", error)
      }
    },
    handleClose() {
      // 关闭弹窗等操作
    }
  }
}
</script>
